package cn.wolfcode.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.CustomerQuery;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequiresPermissions(value = {"customer:potentialList","潜在客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQuery qo){
        qo.setStatus(Customer.STATUS_COMMON);//值查询潜在客户的状态

        //如果登录的员工是管理员或者经理(admin角色或者Market_Manager角色),就可以看所有的客户,
        // 如果只是普通的销售人员,只能看自己的客户,不能看到别人的
        Subject subject = SecurityUtils.getSubject();
        if(!(subject.hasRole("ADMIN")||subject.hasRole("Market_Manager"))){
            Employee employee = (Employee)subject.getPrincipal();
            qo.setSellerId(employee.getId());//当前登录用户的id
        }
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //销售人员的查询下拉框,只查询有Market角色或者Market_Manager角色的员工
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("sellers",sellers);

        //职业下拉框数据
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByParentSn("job");
        model.addAttribute("jobs",jobs);

        //来源下拉框数据
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByParentSn("source");
        model.addAttribute("sources",sources);

        //来源下拉框数据
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByParentSn("communicationMethod");
        model.addAttribute("ccts",ccts);

        return "customer/potentialList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户列表删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户列表新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }

    //验证用户名是否存在
    @RequestMapping("/checkTel")
    @ResponseBody
    public Map<String,Boolean> checkTel(String tel,Long id){
        //通过判断id是否有值,如果有值,是编辑,没有值,是保存,
        HashMap<String,Boolean> map = new HashMap<>();
        if(id!=null){
            //编辑
            //根据id查询名称是否一致,是的话继续验证,不是的话中断
            Customer customer = customerService.get(id);
            //如果id是当前的电话号码和当前一样,没问题,如果和当前不一样,查询数据库的,
            if(customer.getTel().equals(tel)){
                map.put("valid",customer.getTel().equals(tel));
                return map;
            }else{
                //保存,通过名字查找,如果是空的,就继续,不是空的就中断
                Customer customer1 = customerService.selectByTel(tel);
                map.put("valid",customer1==null);
                return map;
            }
        }else{
            //保存,通过名字查找,如果是空的,就继续,不是空的就中断
            Customer customer1 = customerService.selectByTel(tel);
            map.put("valid",customer1==null);
            return map;
        }
    }

    @RequestMapping("/changeStatus")
    @RequiresPermissions(value = {"customer:changeStatus","客户状态修改"},logical = Logical.OR)
    @ResponseBody
    public JsonResult changeStatus(Long id,Integer status){
        Customer customer = customerService.get(id);
        customer.setStatus(status);
        customerService.updateStatus(customer);
        return new JsonResult();
    }

    @RequiresPermissions(value = {"customer:poolList","客户池页面"},logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQuery qo){
        qo.setStatus(Customer.STATUS_POOL);//值查询潜在客户的状态

        //如果登录的员工是管理员或者经理(admin角色或者Market_Manager角色),就可以看所有的客户,
        // 如果只是普通的销售人员,只能看自己的客户,不能看到别人的
        Subject subject = SecurityUtils.getSubject();
        if(!(subject.hasRole("ADMIN")||subject.hasRole("Market_Manager"))){
            Employee employee = (Employee)subject.getPrincipal();
            qo.setSellerId(employee.getId());//当前登录用户的id
        }
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //销售人员的查询下拉框,只查询有Market角色或者Market_Manager角色的员工
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("sellers",sellers);

        //职业下拉框数据
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByParentSn("job");
        model.addAttribute("jobs",jobs);

        //来源下拉框数据
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByParentSn("source");
        model.addAttribute("sources",sources);

        return "customer/poolList";
    }
}
